package id.net.iconpln.bb.client;

import java.util.List;

import id.net.iconpln.bb.client.controller.AppController;
import id.net.iconpln.bb.client.controller.ContentController;
import id.net.iconpln.bb.client.controller.NavigationController;
import id.net.iconpln.bb.client.model.Entry;
import id.net.iconpln.bb.client.model.NavigationModel;
import id.net.iconpln.bb.client.model.UserBean;
import id.net.iconpln.bb.client.service.BatubaraService;
import id.net.iconpln.bb.client.service.BatubaraServiceAsync;
import id.net.iconpln.bb.client.service.PengendalianRPCService;
import id.net.iconpln.bb.client.service.PengendalianRPCServiceAsync;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class Main implements EntryPoint {
	private final BatubaraServiceAsync pbService = GWT.create(BatubaraService.class);
	

	private Dispatcher dispatcher;
	public static final String MODEL = "model";
	private NavigationModel model;
	private String mModule,whoAmI,idPltu,namaUnit,levelUser;

	@Override
	public void onModuleLoad() {			
		pbService.ipBrowser(new AsyncCallback<List<UserBean>>() {
			public void onFailure(Throwable caught) {
				GWT.log("error",null);
			}
			
			@Override
			public void onSuccess(List<UserBean> result)  {	
				GWT.log("main :");
				GWT.log("module :" + result.get(0).getModule(), null);
				GWT.log("id Unit :" + result.get(0).getId_unit(), null);
				GWT.log("User :" + result.get(0).getUser(), null);
				GWT.log("nama Unit :" + result.get(0).getNama_unit(), null);
				GWT.log("Level User :" + result.get(0).getLeveluser(), null);
					
					mModule = result.get(0).getModule();
					whoAmI = result.get(0).getUser();
					idPltu = result.get(0).getId_unit();	
					namaUnit = result.get(0).getNama_unit();	
					levelUser = result.get(0).getLeveluser();	
					
				    model = new NavigationModel();
				    Registry.register(MODEL, model);
				    Registry.register("isSlider", "no");
				    Registry.register("isModule", mModule);	    
				    Registry.register("whoAmI", whoAmI);
				    Registry.register("idPltu", idPltu);
				    Registry.register("namaUnit", namaUnit);
				    Registry.register("levelUser", levelUser);
				    
					dispatcher = Dispatcher.get();
			        dispatcher.addController(new AppController()); 
			        dispatcher.addController(new NavigationController());
			        dispatcher.addController(new ContentController());
			        dispatcher.dispatch(AppEvents.Init);
			        
			        GXT.hideLoadingPanel("loading");
    	
			}

        });
		
     }
	public static void showPage(Entry entry) {
		GWT.log("showPage() ", null);
		
	    AppEvent appEvent = new AppEvent(AppEvents.ShowPage, entry);
	    appEvent.setHistoryEvent(false);
	    appEvent.setToken(entry.getId());
	    Dispatcher.forwardEvent(appEvent);
	  }

}
